package com.cq.hd.merchant.controller;

import com.cq.hd.common.annotation.MerchantLoginUser;
import com.cq.hd.common.entity.MerchantAppLoginUser;
import com.cq.hd.common.entity.wx.dto.WxLoginMobileDto;
import com.cq.hd.common.entity.wx.vo.Code2SessionVo;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.ResultGenerator;
import com.cq.hd.member.api.dto.MerchantLoginDto;
import com.cq.hd.member.api.dto.MerchantSmsLoginDto;
import com.cq.hd.member.api.vo.MerchantLoginVo;
import com.cq.hd.merchant.biz.AuthBiz;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/auth")
@Api(tags = "auth相关接口")
public class AuthController {

    @Autowired
    private AuthBiz authBiz;

    // 根据code，获取openId和sessionKey，根据openId判断用户是否已经注册，如果已注册，则返回token，否则返回openId
    @ApiOperation(value = "wxCode登录校验")
    @ApiResponse(response = Code2SessionVo.class, code = 200, message = "接口返回对象参数")
    @PostMapping("/code")
    public Result<Code2SessionVo> code(String code, HttpServletRequest request) {
        return ResultGenerator.success(authBiz.code2session(code, request));
    }

    // 根据openId+手机号加密数据，解密
    @ApiOperation(value = "获取并解密wx手机号")
    @ApiResponse(response = String.class, code = 200, message = "接口返回对象参数")
    @PostMapping("/getWxMobile")
    public Result<String> getWxMobile(@RequestBody WxLoginMobileDto wxLoginMobileDto) {
        return ResultGenerator.success(authBiz.getWxMobile(wxLoginMobileDto));
    }

    @ApiOperation(value = "用户名密码登录")
    @ApiImplicitParam(name = "merchantLoginDto", value = "登录参数", required = true, paramType = "body", dataType = "MerchantLoginDto")
    @ApiResponse(response = MerchantLoginVo.class, code = 200, message = "接口返回对象参数")
    @PostMapping("/login")
    public Result<MerchantLoginVo> login(@RequestBody MerchantLoginDto merchantLoginDto, HttpServletRequest request) {
        return ResultGenerator.success(authBiz.login(merchantLoginDto, request));
    }

    @ApiOperation(value = "短信验证码登录")
    @ApiImplicitParam(name = "merchantSmsLoginDto", value = "登录参数", required = true, paramType = "body", dataType = "MerchantSmsLoginDto")
    @ApiResponse(response = MerchantLoginVo.class, code = 200, message = "接口返回对象参数")
    @PostMapping("/loginBySmsCode")
    public Result<MerchantLoginVo> loginBySmsCode(@RequestBody MerchantSmsLoginDto merchantSmsLoginDto, HttpServletRequest request) {
        return ResultGenerator.success(authBiz.loginBySmsCode(merchantSmsLoginDto, request));
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public Result<Boolean> logout(@RequestHeader("Authorization") String token, @ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser) {
        return ResultGenerator.success(authBiz.logout(token, merchantAppLoginUser));
    }

}
